{include /common/header}
<script>
    function file_upload(){
        var file = event.target.files[0];
        // 判断是否图片
        if(!file) return ;

        var reader = new FileReader();
        reader.readAsDataURL(file);
        reader.onload = function(e){
            $.ajax({
                url: '{:\mylib\Route::buildUrl('/admin/api/upload')}',
                type: 'POST',
                dataType: 'json',
                data: {
                    name:file.name,
                    file_data:reader.result
                },
                success:function(res){
                    if (res.code) {
                        $('#upload_xx').val(res.data.filename);
                    }else{
                        alert(res.message);
                    }
                },
                error:function(){
                    alert('系统繁忙！');
                }
            });
        }
    }
    function upload_trigger(){
        document.getElementById('upload_form').reset();
        document.getElementById('upload_input').click();
        return false;
    }
</script>
<form id="upload_form" style="display: none;">
    <input type="file" id="upload_input" onchange="file_upload()">
</form>
<form action="{:\mylib\Route::buildUrl('/admin/block/edit')}" method="POST">
    <fieldset>
        <legend>编辑碎片</legend>

        <div class="form-group">
            <label>分组</label>
            <input type="text" class="form-control" name="group" value="{$block.group}" required>
        </div>

        <div class="form-group">
            <label>标题</label>
            <input type="text" class="form-control" name="title" value="{$block.title}" required>
        </div>

        <div class="form-group">
            <label>名称</label>
            <input type="text" class="form-control" name="name" value="{$block.name}" required>
            <p class="help-block">英文字符</p>
        </div>

        <div class="form-group">
            {switch $block['type']}
                {case 'text'}
                <label>文本</label>
                <textarea class="form-control" name="value" rows="3">{$block.value}</textarea>
                {/case}

                {case 'editor'}
                <label>内容</label>
                <textarea name="value" rows="8" class="form-control xheditor {urlType:'abs',html5Upload:true,upMultiple:true,upLinkUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upLinkExt:'zip,rar,txt',upImgUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upImgExt:'jpg,jpeg,gif,png',upFlashUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upFlashExt:'swf',upMediaUrl:'{:\mylib\Route::buildUrl('/admin/api/xhupload')}',upMediaExt:'avi'}">{$block.value}</textarea>
                {/case}

                {case 'upload'}
                <label>附件</label>
                <div class="input-group">
                    <input type="text" class="form-control" name="value" value="{$block.value}" id="upload_xx" placeholder="">
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" onclick="upload_trigger()">上传</button>
                    </span>
                </div>
                {/case}

                {case 'tpl'}
                <label>模板</label>
                <textarea class="form-control" name="value" rows="8">{$block.value}</textarea>
                <p class="help-block">支持模板标签 支持php代码</p>
                {/case}
            {/switch}
        </div>

        <div class="form-group">
            <label>缓存有效期</label>
            <input type="number" class="form-control" name="expire" value="{$block.expire}" required>
            <p class="help-block">0表示不缓存 单位秒</p>
        </div>

        <div class="form-group">
            <label>备注</label>
            <input type="text" class="form-control" name="tips" value="{$block.tips}">
        </div>

        <input type="hidden" name="id" value="{$block.id}">
        <button type="submit" class="btn btn-primary">提交</button>
    </fieldset>
</form>
{include /common/footer}